package com.spic.business.hidden.special.controller;

import com.spic.business.common.core.utils.poi.BusinessExcelUtil;
import com.spic.business.hidden.special.domain.HiddenSpecialScreenProgram;
import com.spic.business.hidden.special.service.IHiddenSpecialScreenProgramService;
import com.spic.common.core.utils.StringUtils;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import com.spic.system.api.RemoteDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 隐患排查计划Controller
 *
 * @author yangjie
 * @date 2021-10-14
 */
@RestController
@RequestMapping("/hidden/special")
public class HiddenSpecialScreenProgramController extends BaseController
{
    @Autowired
    private IHiddenSpecialScreenProgramService hiddenSpecialScreenProgramService;
    @Autowired
    private RemoteDeptService remoteDeptService;
    /**
     * 查询隐患排查计划列表
     */
    @PreAuthorize(hasPermi = "business/hidden:special:list")
    @GetMapping("/list")
    public TableDataInfo list(HiddenSpecialScreenProgram hiddenScreenProgram)
    {
        //获取部门及子部门
        if (StringUtils.isNotEmpty(hiddenScreenProgram.getDept())){
            List<Long> depts = remoteDeptService.getDeptAndChildren(Long.parseLong(hiddenScreenProgram.getDept())).getData();
            hiddenScreenProgram.setDepts(depts);
        }
        startPage();
        List<HiddenSpecialScreenProgram> list = hiddenSpecialScreenProgramService.selectHiddenScreenProgramList(hiddenScreenProgram);
        return getDataTable(list);
    }

    /**
     * 导出隐患排查计划列表
     */
    @PreAuthorize(hasPermi = "business/hidden:special:export")
    @Log(title = "隐患排查计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HiddenSpecialScreenProgram hiddenScreenProgram) throws IOException
    {
        List<HiddenSpecialScreenProgram> list = hiddenSpecialScreenProgramService.selectHiddenScreenProgramList(hiddenScreenProgram);
        ExcelUtil<HiddenSpecialScreenProgram> util = new BusinessExcelUtil<HiddenSpecialScreenProgram>(HiddenSpecialScreenProgram.class);
        util.exportExcel(response, list, "隐患排查计划数据");

    }

    /**
     * 获取隐患排查计划详细信息
     */
    @PreAuthorize(hasPermi = "business/hidden:special:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(hiddenSpecialScreenProgramService.selectHiddenScreenProgramById(id));
    }

    /**
     * 新增隐患排查计划
     */
    @PreAuthorize(hasPermi = "business/hidden:special:add")
    @Log(title = "隐患排查计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HiddenSpecialScreenProgram hiddenScreenProgram)
    {
        return toAjax(hiddenSpecialScreenProgramService.insertHiddenScreenProgram(hiddenScreenProgram));
    }

    /**
     * 修改隐患排查计划
     */
    @PreAuthorize(hasPermi = "business/hidden:special:edit")
    @Log(title = "隐患排查计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HiddenSpecialScreenProgram hiddenScreenProgram)
    {
        return toAjax(hiddenSpecialScreenProgramService.updateHiddenScreenProgram(hiddenScreenProgram));
    }

    /**
     * 删除隐患排查计划
     */
    @PreAuthorize(hasPermi = "business/hidden:special:remove")
    @Log(title = "隐患排查计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hiddenSpecialScreenProgramService.deleteHiddenScreenProgramByIds(ids));
    }

    @GetMapping("/getLatest")
    public AjaxResult getLatest() {
        return AjaxResult.success(hiddenSpecialScreenProgramService.getLatest());
    }
}
